package com.useeinfo.oa.modules.base.biz;

import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.oa.modules.base.dao.DataCatDao;
import com.useeinfo.oa.modules.base.entity.DataCat;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : bin.liu
 * @Date: 2018/12/12 15:14
 * @Description: 资料分类管理service
 */
@Service
public class DataCatBiz extends CrudBiz<DataCatDao, DataCat> {

    public Boolean existDataCatName(DataCat dataCat, Long enterpriseId) {
        if (StringUtils.isBlank(dataCat.getDataCatName())) {
            return true;
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("enterpriseId", enterpriseId+"");
        queryParam.getSqlMap().put("dataCatName", dataCat.getDataCatName());
        List<DataCat> dataCatList = this.findList(queryParam);

        if (dataCat.getId() == null) {
            return CollectionUtils.isNotEmpty(dataCatList);
        } else {
            return !((dataCatList.size() == 1 && dataCatList.get(0).getId().equals(dataCat.getId())) || dataCatList.size() == 0);
        }
    }

    public List<DataCat> findModeIn(List<String> ids){
        if (CollectionUtils.isEmpty(ids)) {
            return new ArrayList<>();
        }
        QueryParam queryParam = new QueryParam();
        queryParam.getSqlMap().put("ids", ids + "");

        List<DataCat> dataCatList = dao.findList(queryParam);
        return dataCatList;
    }
}
